package com.ld.admin.controller.oa;

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.oa.SysUserSeatsModel;
import com.ld.admin.model.system.SystemUser;
import com.ld.admin.service.oa.SysUserSeatsService;
import com.ld.shieldsb.annotation.NoLog;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.StaticParams;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 座次表
 * 
 * @ClassName SysUserSeatsController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年04月17日 11:39:34
 *
 */
@Api(tags = "座次表")
@Controller(value = "oaSysUserSeatsController")
@RequestMapping("${shieldsb.adminPath}/oa/seats")
public class SysUserSeatsController extends AdminBasicController<SysUserSeatsModel> {
    @Autowired
    protected SysUserSeatsService sysUserSeatsService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        QueryModel queryModel = sysUserSeatsService.getSearchQueryModel(request);
        // 1、设置QueryLink为true
//        queryModel.setQueryLink(true);
//        List<SysUserSeatsModel> list = sysUserSeatsService.getList(request, getModelClass(), queryModel);
        // 1.1、如果想选择部分关联对象，在此设置对象，需要有第一步做为前提
//        queryModel.addQueryField(SysUserSeatsModel::getUser); // 手动设置选择对象

        // 2、可独立使用，不需要设置QueryLink为true，查询所有注解的关联对象
        sysUserSeatsService.getListAndLinks(request, getModelClass(), queryModel);
        // 3、返回结果后，fetchLinks查询指定的关联对象
//        List<SysUserSeatsModel> list = sysUserSeatsService.getList(request, getModelClass(), queryModel);
//        sysUserSeatsService.fetchLinks(list, SysUserSeatsModel::getUser, queryModel); // 如果搜索的管理对象超过1个，通过queryModel.addQueryField添加
        Integer type = getInt("type");
        return "a/oa/seats/sysuserseats_list" + (type == 0 ? "" : type);
    }

    @ApiOperation(value = "座次表展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        sysUserSeatsService.getOne(request, getModelClass(), id);
        return "a/oa/seats/sysuserseats_show";
    }

    @ApiOperation(value = "跳转到添加/修改座次表", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        setAttributes();
        Long id = getLong("id");
        sysUserSeatsService.getNormalOne(request, getModelClass(), id);
        return "a/oa/seats/sysuserseats_update";
    }

    @ApiOperation(value = "添加/修改座次表", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid SysUserSeatsModel model) {
        model.setState(StaticParams.STATE_NORMAL); // 可能存在删除的情况
        Result result = sysUserSeatsService.addOrUpdate(model, sysUserSeatsService.existById(getModelClass(), model.getId()));
        return Web.Response.result(result);
    }

    @ApiOperation(value = "保存座次信息", notes = "")
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object save() {
        String params = getString("param");
        Result result = sysUserSeatsService.save(params);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除座次表", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        Long id = getLong("id");
        Result result = sysUserSeatsService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

    @NoLog("aop日志不记录")
    @ApiOperation(value = "获取用户", notes = "")
    @RequestMapping(path = "/getUsers", method = RequestMethod.POST)
    public String getUsers() {
        String name = getString("name");
        Long id = getLong("id");
        QueryModel queryModel = sysUserSeatsService.getSearchQueryModel(request);
        if (StringUtils.isNotBlank(name)) {
            name = getString("name").trim();
            queryModel.addLike("userName", name);
        } else {
            queryModel.addEq("id", id);
        }
        List<SystemUser> list = sysUserSeatsService.getList(SystemUser.class, queryModel, 20);
        request.setAttribute("users", list);
        return "a/oa/seats/include_users";
    }

    @NoLog("aop日志不记录")
    @ApiOperation(value = "获取json对象", notes = "")
    @RequestMapping(path = "/getUserJson", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object getUserJson() {
        Long id = getLong("id");
        SystemUser result = sysUserSeatsService.getOne(SystemUser.class, id);
        return Web.Response.success("", result);
    }

}